//
//  AyIconView.m
//  xWeibo
//
//  Created by alvis mac on 11/5/15.
//  Copyright © 2015 alvis. All rights reserved.
//

#import "AyIconView.h"
#import "AyDefine.h"
#import "UIImageExtend.h"
#import "UIImageViewExtend.h"

@interface AyIconView()
    {
        UIImageView *_imageView; // 头像
        NSString *_imagePlaceholder;
        UIImageView *_verifiedView; // 认证
    }
    @property (nonatomic, strong) AyUser *user;
    @property (nonatomic, assign) IWIconType iconType;
@end

@implementation AyIconView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setup];
    
    if (_user) {
        [self setUser:_user iconType:_iconType];
    }
}

- (void)setup
{
    _imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    
    _verifiedView = [[UIImageView alloc] init];
    _verifiedView.bounds = CGRectMake(0, 0, IWVerifiedWH, IWVerifiedWH);
    [self addSubview:_verifiedView];
}

- (void)setIconType:(IWIconType)iconType
{
    _iconType = iconType;
    
    CGFloat iconWH;
    switch (iconType) {
        case IWIconTypeDefault:
            iconWH = IWIconWH;
            _imagePlaceholder = @"avatar_default.png";
            [_imageView setImageWithURL:_user.avatar_large place:[UIImage imageWithName:_imagePlaceholder]];
            break;
        case IWIconTypeSmall:
            iconWH = IWIconSmallWH;
            _imagePlaceholder = @"avatar_default_small.png";
            [_imageView setImageWithURL:_user.profile_image_url place:[UIImage imageWithName:_imagePlaceholder]];
            break;
        case IWIconTypeBig:
            iconWH = IWIconBigWH;
            _imagePlaceholder = @"avatar_default_big.png";
            [_imageView setImageWithURL:_user.avatar_hd place:[UIImage imageWithName:_imagePlaceholder]];
            _imageView.layer.shadowColor = [UIColor grayColor].CGColor;
            _imageView.layer.shadowOffset = CGSizeMake(1, 1);
            _imageView.layer.shadowOpacity = 0.7;
            break;
    }
    
    _imageView.frame = CGRectMake(0, 0, iconWH, iconWH);
    _verifiedView.center = CGPointMake(iconWH - 2, iconWH - 2);
    
    CGRect frame = self.frame;
    frame.size = CGSizeMake(CGRectGetMaxX(_verifiedView.frame), CGRectGetMaxY(_verifiedView.frame));
    [super setFrame:frame];
}

- (void)setUser:(AyUser *)user
{
    _user = user;
    
    // 认证
    NSString *imageName = nil;
    switch (user.verified_type) {
        case IWVerifiedTypeNone:
            _verifiedView.hidden = YES;
            return;
            
        case IWVerifiedTypeDaren:
            imageName = @"avatar_grassroot.png";
            break;
            
        case IWVerifiedTypePersonal:
            imageName = @"avatar_vip.png";
            break;
            
        default:
            imageName = @"avatar_enterprise_vip.png";
            break;
    }
    _verifiedView.hidden = NO;
    _verifiedView.image = [UIImage imageWithName:imageName];
}

- (void)setUser:(AyUser *)user iconType:(IWIconType)iconType
{
    self.user = user;
    self.iconType = iconType;
}

- (void)setFrame:(CGRect)frame
{
    frame.size = self.frame.size;
    [super setFrame:frame];
}

- (void)setBounds:(CGRect)bounds
{
    bounds.size = self.bounds.size;
    [super setBounds:bounds];
}

#pragma mark 返回头像的宽高
+ (CGSize)sizeWithIconType:(IWIconType)iconType
{
    CGFloat iconWH = 0;
    switch (iconType) {
        case IWIconTypeDefault:
            iconWH = IWIconWH;
            break;
        case IWIconTypeSmall:
            iconWH = IWIconSmallWH;
            break;
        case IWIconTypeBig:
            iconWH = IWIconBigWH;
            break;
    }
    iconWH += IWVerifiedWH * 0.5;
    return CGSizeMake(iconWH, iconWH);
}

@end
